
grErp.controller('CaigouerCtrl',['$scope','$filter','$timeout','CaigouerFactory','inventoryFactory',function($scope,$filter,$timeout,CaigouerFactory,inventoryFactory){
    flashlist=function(){
        $("#refreshbtn").click();
    }
    document.onkeydown = function(e) {
        switch (e.keyCode) {
            case 82:
                flashlist();
                break;
            case 119:
                flashlist();
                break;
        }
    }
    /**
     * 页面初始化
     * @param batch
     */
    $scope.assoinit=function(batch){
        loadingStart();
        $scope.isVaild=false;
        CaigouerFactory.ajaxGetCaigouListByBatch(batch).then(function(d){
            angular.forEach(d,function(data,index){
                for(proper in data){
                    if(!$filter('isValidValue')(data[proper])){
                        d[index][proper]=0;
                    }
                }
                data['trans_fee']=0;
                data['loss']=0;
                data['percent']=1;
                data['cost_off']=0;
                data['total_weight']=$filter('strToNum')(data['total_weight']);
                data['total_num']=$filter('strToNum')(data['total_num']);
                data['unit_num_price']=$filter('strToNum')(data['unit_num_price']);
                data['unit_weight_price']=$filter('strToNum')(data['unit_weight_price']);
                data['unit_box_fee']=$filter('strToNum')(data['unit_box_fee']);
                data['unit_box_weight']=$filter('strToNum')(data['unit_box_weight']);
            })
            $scope.batchgoods=d;
            loadingEnd();
        });
        inventoryFactory.getproductlist('ybz').then(function(d){
            $scope.productlist=d;
        });
    }
    /**
     * 刷新erp商品列表
     */
    $scope.refreshProductList=function(){
        $scope.refreshing=true;
        inventoryFactory.getproductlist('ybz').then(function(d){
            $scope.productlist=d;
            $scope.refreshing=false;
        });
    }
    /**
     * 统一报损
     */
    $scope.lossChange=function(){
        if($scope.loss>1){
            $scope.loss=1;
        }
        if($scope.loss<0){
            $scope.loss=0;
        }
        if(isNaN($scope.loss)){
            $scope.loss=0
        }
        angular.forEach($scope.batchgoods,function(data,index){
            data['loss']=$scope.loss;
        })
    }
    /**
     *统一运费
     */
    $scope.transFeeChange=function(){
        if($scope.trans_fee<0){
            $scope.trans_fee=0;
        }
        angular.forEach($scope.batchgoods,function(data,index){
            data['trans_fee']=$scope.trans_fee;
        })
    }
    /**
     * erp商品选择事件
     * @param listindex
     * @param proindex
     */
    $scope.proselect=function(listindex,proindex){
        if($filter('isValidValue')(proindex)){
            var erpSpec = $filter('getSpecUnit')($scope.productlist[proindex].spec);
            var goodSpec = $filter('getSpecUnit')($scope.batchgoods[listindex].spec);
            var erpSpecNum=$filter('strToNum')($scope.productlist[proindex].spec);
            var goodSpecNum=$filter('strToNum')($scope.batchgoods[listindex].spec);
            var normalUnit=['斤','个','头']
            if(erpSpec.indexOf(normalUnit)==-1){
                $scope.batchgoods[listindex].warn=true;
                $scope.batchgoods[listindex].warn_info="不常见的规格单位";
            }
            if(erpSpec!=goodSpec){
                $scope.batchgoods[listindex].warn=true;
                $scope.batchgoods[listindex].warn_info="规格单位不一致";
            }
            if(erpSpecNum!=goodSpecNum){
                $scope.batchgoods[listindex].warn=true;
                $scope.batchgoods[listindex].warn_info="规格数量不一致";
            }
            $scope.batchgoods[listindex].erp_spec=$scope.productlist[proindex].spec;
            $scope.batchgoods[listindex].erp_pname=$scope.productlist[proindex].pname;
            $scope.batchgoods[listindex].BIN=$scope.productlist[proindex].BIN;
        }else{
            $scope.batchgoods[listindex].BIN="";
            $scope.batchgoods[listindex].erp_spe="";
            $scope.batchgoods[listindex].erp_pname="";
        }
        $scope.validDataVerify();
    }
    /**
     *数据列表数据可计算性校验
     */
    $scope.validDataVerify=function(){
        var vaildtmp=true;
        angular.forEach($scope.batchgoods,function(data,index){
            var typeObj=typeResult(data)
            if($filter('isValidValue')(data.BIN)&&$filter('isValidValue')(data.erp_spec)&&typeObj.result){
                data['valid']=true;
                data['type_json']=typeObj.data;
                var typeArr=JSON.parse(data['type_json']);
                var str="";
                for(typeindex in typeArr){
                    switch (typeArr[typeindex]){
                        case 'jmjm':
                            if(typeindex==0){
                                str+="斤买斤卖，可选择";
                            }
                            break;
                        case 'xmjm':
                            if(typeindex==0){
                                str+="箱买斤卖，可选择";
                            }else{
                                str+="箱买斤卖、"
                            }
                            break;
                        case 'xmgm':
                            if(typeindex==0){
                                str+="箱买个卖，可选择";
                            }else{
                                str+="箱买个卖、"
                            }
                            break;
                        case 'jmgm':
                            if(typeindex==0){
                                str+="斤买个卖";
                            }else{
                                str+="斤买个卖、"
                            }
                            break;
                    }
                }
                data['help']=str;
            }else{
                data['valid']=false;
                vaildtmp=false;
                if(!typeObj.result){
                    data['help']="无法计算基础价格";
                }
                if(!$filter('isValidValue')(data.BIN)){
                    data['help']="没有选择erp商品";
                }
            }
        });
        $scope.isVaild=vaildtmp;
    }
    $scope.quicksave=function(index){
        console.log($scope.batchgoods[index]);
        if($scope.batchgoods[index].valid==true){
            CaigouerFactory.ajaxPostQuickSave($scope.batchgoods[index]).then(function(d){
                if(d==1){
                    alert("保存成功");
                }else{
                    alert("保存失败");
                }
            })
        }else{
            alert("该数据校验结果不正确，无法保存");
        }
    }
    /**
     * 计算方式
     * @param data
     * @returns {{计算结果json，是否可计算}}
     */
    typeResult=function(data){
        var Obj={};
        var arr=[];
        var result=false;
        if(jmjm(data)){
            arr.push('jmjm');
            result=true;
        }
        if(xmjm(data)){
            arr.push('xmjm');
            result=true;

        }
        if(xmgm(data)){
            arr.push('xmgm');
            result=true;
        }
        if(jmgm(data)){
            arr.push('jmgm');
            result=true;

        }
        Obj.data=JSON.stringify(arr);
        Obj.result=result
        return Obj
    }
    /**
     *斤买个卖
     */
    jmgm=function(data){
        var unit=$filter('getSpecUnit')(data.erp_spec);
        if(isNaN(unit)){
            if(unit!="斤"&&data.total_weight>0&&data.unit_weight_price>0&&data.total_num>0){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    /**
     *箱买个卖
     */
    xmgm=function(data){
        var unit=$filter('getSpecUnit')(data.erp_spec);
        if(isNaN(unit)){
            if(unit!="斤"&&data.unit_num_price>0&&data.total_num>0){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    /**
     *箱买斤卖
     */
    xmjm=function(data){
        if(data.total_num>0&&data.unit_num_price>0&&data.total_weight>0){
            return true;
        }else{
            return false;
        }
    }
    /**
     *斤买斤卖
     */
    jmjm=function(data){
        if(data.total_num>0&&data.unit_weight_price>0&&data.total_weight>0){
            return true;
        }else{
            return false;
        }
    }
}]);
grErp.factory('CaigouerFactory',['$http','$q',function($http,$q){
    var service={};
    service.ajaxGetCaigouListByBatch=function(batch){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Admin/Caigouer/ajaxGetCaigouListByBatch",
            params:{batch:batch}
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    }
    service.ajaxPostQuickSave=function(data){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Admin/Caigouer/ajaxPostQuickSave",
            params:{data:data}
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    }
    return service;

}])